package com.zy.component;

import com.zy.util.MD5Util;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @Author: zy 690748807@qq.com
 * @Description: TODO
 * @Date: 2024/2/21 21:14
 * @Version: 1.0
 * @name 方法名字：安全框架的密码编码器的实现类
 */
@Component
public class PasswordEncoderImpl implements PasswordEncoder {
 /**
 *@Author: zy 690748807@qq.com
 * @Param:
 * @return:
 * @Date: 2024/2/21
 * @Description: 对原文密码加密成密文密码
 */
    @Override
    public String encode(CharSequence rawPassword) {

        String digest = MD5Util.digest(rawPassword.toString());

        return digest;
    }
 /**
 *@Author: zy 690748807@qq.com
 * @Param:
 * @return:
 * @Date: 2024/2/21
 * @Description: 比较密码是否一致
 */
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        String digest = MD5Util.digest(rawPassword.toString());

        return encodedPassword.equals(digest);
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        for (int i = 0; i < 3; i++) {
            //测试加盐的加密方式生成的密码是否一样
            String encode = bc.encode("123456");
            System.out.println(encode);
        }

    }
}
